/**
 * 怪物的词条系统
*/
const MonsterNbt = "eou_monster_nbt"
/**
 * 进行怪物初始化
 * 初始化等级
 * 初始化稀有度
 * 初始化倾向性
 * 初始化名称
 * 初始化词条
*/
// EntityEvents.spawned(event => {
//     var Entity = event.getEntity()
//     if (Entity.isMonster()) {//不是怪物或者完成了初始化的不进行下一步
//         if (!Entity.forgePersistentData.get(MonsterNbt)) {
//             if (Entity.getType() == "minecraft:zombie") {
//                 let Data = new $CompoundTag()
//                 let Name = [Entity.getName()]
//                 global.EntryHelper.AllEntryGroup["level_group"].apply(Data, Name, Entity)
//                 // global.EntryHelper.AllEntryGroup["rarity_group"].apply(Data, Name, Entity)
//                 // global.EntryHelper.AllEntryGroup["biome_group"].apply(Data, Name, Entity)
//                 // global.EntryHelper.AllEntryGroup["monster_group"].apply(Data, Name, Entity)
//                 // global.EntryHelper.AllEntryGroup["type_group"].apply(Data, Name, Entity)
//                 // global.EntryHelper.AllEntryGroup["random_group"].apply(Data, Name, Entity)
//                 for (let i = 0; i < Data.allKeys.length; i++) {//词条初始化
//                     let entry = global.EntryHelper.AllEntry[Data.allKeys[i]]
//                     let level = Data.get(Data.allKeys[i]).level
//                     if (entry != undefined && level != undefined) {
//                         entry.apply(Data, Entity, level)
//                     }
//                 }
//                 Entity.setCustomName(Name)//设置自定义名称
//                 Entity.forgePersistentData.put(MonsterNbt, Data)//存储自定义信息
//             }
//         } else {
//             let Data = Entity.forgePersistentData.get(MonsterNbt)
//             for (let i = 0; i < Data.allKeys.length; i++) {//词条初始化
//                 let entry = global.EntryHelper.AllEntry[Data.allKeys[i]]
//                 let level = Data.get(Data.allKeys[i]).level
//                 if (entry != undefined && level != undefined) {
//                     entry.apply(Data, Entity, level)
//                 }
//             }
//             Entity.forgePersistentData.put(MonsterNbt, Data)
//         }
//     }
// })

ServerEvents.tick(event => {
    event.server.entities.forEach(Entity => {
        if (Entity.isMonster()) {
            let Data = Entity.forgePersistentData.get(MonsterNbt)
            if (Data != undefined) {
                for (let i = 0; i < Data.allKeys.length; i++) {//执行tick词条
                    let entry = global.EntryHelper.AllEntry[Data.allKeys[i]]
                    let level = Data.get(Data.allKeys[i]).level
                    if (entry != undefined && level != undefined) {
                        let a = entry.tick(Data, Entity, level)
                        if (a == global.EntryResult.succeed()) {
                            break
                        }
                    }
                }
                Entity.forgePersistentData.put(MonsterNbt, Data)
            }
        }
    });
})
EntityEvents.death(event => {
    let Entity = event.getEntity()
    if (Entity.isMonster()) {
        let Data = Entity.forgePersistentData.get(MonsterNbt)
        if (Data != undefined) {
            for (let i = 0; i < Data.allKeys.length; i++) {//执行死亡词条
                let entry = global.EntryHelper.AllEntry[Data.allKeys[i]]
                let level = Data.get(Data.allKeys[i]).level
                if (entry != undefined && level != undefined) {
                    let a = entry.death(Data, Entity, level)
                    if (a == global.EntryResult.succeed()) {
                        break
                    } else if (a == global.EntryResult.error()) {
                        Entity.forgePersistentData.put(MonsterNbt, Data)
                        Entity.setHealth(Entity.maxHealth)
                        event.cancel()
                    }
                }
            }
            Entity.forgePersistentData.put(MonsterNbt, Data)
        }
    }
})
global.defense = function (event) {
    let Entity = event.getEntity()
    let message = { damage: event.getAmount(), source: event.source }
    if (Entity.isMonster()) {
        let Data = Entity.forgePersistentData.get(MonsterNbt)
        if (Data != undefined) {
            for (let i = 0; i < Data.allKeys.length; i++) {//执行防御词条
                let entry = global.EntryHelper.AllEntry[Data.allKeys[i]]
                let level = Data.get(Data.allKeys[i]).level
                if (entry != undefined && level != undefined) {
                    let a = entry.defense(Data, Entity, level, message)
                    if (a == global.EntryResult.succeed()) {
                        break
                    } else if (a == global.EntryResult.error()) {
                        Entity.forgePersistentData.put(MonsterNbt, Data)
                        event.setCanceled(true)
                    }
                }
            }
            event.setAmount(message.damage)
            Entity.forgePersistentData.put(MonsterNbt, Data)
        }
    }
}
global.attack = function (event) {
    let Entity = event.source.actual
    if (!(Entity instanceof $LivingEntity)) {
        return
    }
    let message = { damage: event.getAmount(), source: event.source }
    // console.log("Entity:" + Entity)
    if (Entity.isMonster()) {
        let Data = Entity.forgePersistentData.get(MonsterNbt)
        if (Data != undefined) {
            console.log("Data:" + Data)
            for (let i = 0; i < Data.allKeys.length; i++) {//执行攻击词条
                let entry = global.EntryHelper.AllEntry[Data.allKeys[i]]
                let level = Data.get(Data.allKeys[i]).level
                if (entry != undefined && level != undefined) {
                    let a = entry.attack(Data, Entity, level, message)
                    if (a == global.EntryResult.succeed()) {
                        break
                    } else if (a == global.EntryResult.error()) {
                        Entity.forgePersistentData.put(MonsterNbt, Data)
                        event.setCanceled(true)
                    }
                }
            }
            event.setAmount(message.damage)
            Entity.forgePersistentData.put(MonsterNbt, Data)
        }
    }
}
global.heal = function (event) {
    let Entity = event.getEntity()
    let message = { amount: event.getAmount() }
    if (Entity.isMonster()) {
        let Data = Entity.forgePersistentData.get(MonsterNbt)
        if (Data != undefined) {
            for (let i = 0; i < Data.allKeys.length; i++) {//执行治疗词条
                let entry = global.EntryHelper.AllEntry[Data.allKeys[i]]
                let level = Data.get(Data.allKeys[i]).level
                if (entry != undefined && level != undefined) {
                    let a = entry.heal(Data, Entity, level, message)
                    if (a == global.EntryResult.succeed()) {
                        break
                    } else if (a == global.EntryResult.error()) {
                        Entity.forgePersistentData.put(MonsterNbt, Data)
                        event.setCanceled(true)
                    }
                }
            }
            event.setAmount(message.amount)
            Entity.forgePersistentData.put(MonsterNbt, Data)
        }
    }
}